using System;
using System.Text.RegularExpressions;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace Infrastructure
{
    /// <summary>
    /// 获取配置信息
    /// </summary>
    public static class Config
    {
        /// <summary>
        /// 读取配置文件的配置项。
        /// 配置文件默认为appsettings.json。
        /// 配置项格式为[section:...childSection:name]，如Logging:Model:Name。
        /// </summary>
        /// <param name="section">需要读取的配置项,不区分大小写。</param>
        /// <param name="file">配置文件的名称。</param>
        /// <returns>配置项的值。未读取到值将返回null。</returns>
        public static string ReadSetting(string section, string file = "appsettings.json")
        {
            var builder = new ConfigurationBuilder()
            .SetBasePath(PathHelper.GetBaseDirectory())
            .AddJsonFile(file);

            var config = builder.Build();

            return config[section];
        }

        /// <summary>
        /// 读取配置文件的配置节。
        /// 配置文件默认为appsettings.json。
        /// </summary>
        /// <param name="section">需要读取的配置节,不区分大小写。</param>
        /// <param name="file">配置文件的名称。</param>
        /// <returns>配置节对象。</returns>
        public static IConfigurationSection GetSection(string section, string file = "appsettings.json")
        {
            var builder = new ConfigurationBuilder()
            .SetBasePath(PathHelper.GetBaseDirectory())
            .AddJsonFile(file);

            var config = builder.Build();

            return config.GetSection(section);
        }
    }
}